package com.chunuo.aclservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.aclservice.entity.Permission;
import com.chunuo.aclservice.entity.Role;
import com.chunuo.aclservice.entity.RolePermission;
import com.chunuo.aclservice.mapper.RolePermissionMapper;
import com.chunuo.aclservice.service.PermissionService;
import com.chunuo.aclservice.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-05
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private PermissionService permissionService;

    /**
     * 更新角色与权限的 绑定
     * @param roleId
     * @param permissionIds
     */
    @Override
    public void batchBind(String roleId, List<String> permissionIds) {
        //先删除原先的绑定
        this.removeById(roleId);

        // 将角色和权限的id封装到list中，最后批量添加
        List<RolePermission> rolePermissions = new ArrayList<>();
        for(String id:permissionIds){
            if(StringUtils.isEmpty(id)) continue;
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(id);

            rolePermissions.add(rolePermission);
        }
        //批量添加
        this.saveBatch(rolePermissions);
    }






}
